In [1]:
%load_ext vimception



In [1]:
%load_ext autoreload
%autoreload 2

In [2]:
%reload_ext autoreload

In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Hidden Alignment CRF for spelling correction

This is an example to show how the model can be used to score candidate corrections.

Training data

Fei Liu and others collected string pairs from social media and this excellent source is available at:

http://www.hlt.utdallas.edu/~yangl/data/Text_Norm_Data_Release_Fei_Liu/


In [4]:
lines = open('examples/Test_Set_3802_Pairs.txt', 'r').readlines()
ppairs = [line.split('\t')[1].strip().split(' | ') for line in lines]
ppairs = [(pair[0], pair[i]) for pair in ppairs for i in xrange(1, len(pair))]
print len(ppairs)
ppairs[:5]


3974
Out[4]:
[('0kkay', 'okay'),
 ('0n', 'on'),
 ('0neee', 'one'),
 ('0r', 'or'),
 ('1s', 'once')]

Let's keep 1000 of these pairs out to evaluate the final performance of the model.


In [5]:
from sklearn.cross_validation import train_test_split
ppairs_train, ppairs_test = train_test_split(ppairs, test_size=1000, random_state=1)
ppairs_train = [tuple(ppair_train) for ppair_train in ppairs_train]
ppairs_test = [tuple(ppair_test) for ppair_test in ppairs_test]
print len(ppairs_train), len(ppairs_test)


2974 1000

Negative training examples

The model needs both positive examples (examples of matching string pairs) and negative examples (examples of strings that do not match). To generate the negative examples, let's just pair shuffle the positive examples:


In [6]:
from numpy.random import shuffle 
incorrect = list(zip(*ppairs_train)[0])
shuffle(incorrect)
correct = list(zip(*ppairs_train)[1])
npairs_train = zip(incorrect, correct)
npairs_train[:5]


Out[6]:
[('mvmnts', 'uses'),
 ('saught', 'mummy'),
 ('sammich', 'shit'),
 ('chiilin', 'transfered'),
 ('likin', 'morning')]

Concatenate the positive and negative examples and create labels - 0 for matching pairs and 1 for non-matching pairs.


In [7]:
x_raw = ppairs_train + npairs_train
y_orig = [0] * len(ppairs_train) + [1] * len(npairs_train)

Extract features

For this example let's just extract all possible features instead of doing feature selection.


In [9]:
from pyhacrf import StringPairFeatureExtractor

In [10]:
fe = StringPairFeatureExtractor(match=True, numeric=True, transition=True)
x_orig = fe.fit_transform(x_raw)

Split off testing examples to evaluate classification


In [44]:
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
x_train, x_test, y_train, y_test = train_test_split(x_orig, y_orig, test_size=0.2, random_state=42)
print y_train[:10], y_test[:10]
print len(y_train), len(x_train), len(y_test), len(x_test)


[0, 0, 0, 0, 1, 1, 1, 1, 1, 0] [1, 1, 0, 1, 1, 1, 1, 1, 0, 1]
4758 4758 1190 1190

Cross-validation to find regularization parameter


In [12]:
from pyhacrf import Hacrf
from scipy.optimize import fmin_l_bfgs_b

In [69]:
models = []
accs_train = []
accs_val = []
regs = []

In [70]:
for n, i in enumerate(np.linspace(-1, 1, 20)):
    for repeat in xrange(5):
        print n, r, 10.0**(i)
        m = Hacrf(l2_regularization=10.0**(i), optimizer=fmin_l_bfgs_b, optimizer_kwargs={'maxfun': 100})

        x_t, x_v, y_t, y_v = train_test_split(x_train, y_train, test_size=0.5, random_state=42 + n + repeat * 1000)

        m.fit(x_t, y_t, verbosity=20)

        train_score = accuracy_score(m.predict(x_t), y_t)
        val_score = accuracy_score(m.predict(x_v), y_v)
        print 10.0**(i), train_score, val_score

        regs.append(10.0**(i))
        models.append(m)
        accs_train.append(train_score)
        accs_val.append(val_score)


0.1
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.06e+04
        20     -128.3      852.4
        40     -42.98      271.7
        60     -37.34      74.94
        80      -35.7      38.01
       100     -35.46      17.73
0.367879441171 1.0 0.974358974359
0.1
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.065e+04
        20     -151.2  1.223e+03
        40 -8.812e+03  8.261e+04
        60     -36.62      99.96
        80     -34.47      70.43
       100     -33.59      35.17
0.367879441171 1.0 0.970996216898
0.1
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.062e+04
        20     -133.0      876.6
        40     -47.92      419.3
        60     -39.87       66.4
        80     -37.99       45.8
       100     -37.53      24.33
0.367879441171 0.999159310635 0.97141656158
0.1
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.033e+04
        20     -107.3  1.099e+03
        40     -42.19      169.6
        60     -33.82      73.29
        80     -32.44      31.54
       100     -31.99      20.71
0.367879441171 1.0 0.974358974359
0.1
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.053e+04
        20     -118.5      829.5
        40     -41.51      130.4
        60     -36.79       69.5
        80     -36.13      31.75
       100     -35.93       15.4
0.367879441171 0.999159310635 0.970575872215
0.12742749857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.065e+04
        20     -151.4  1.241e+03
        40     -278.4  7.459e+03
        60      -42.1      129.7
        80     -39.65      42.84
       100     -39.07       31.7
0.408715141106 1.0 0.970575872215
0.12742749857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.062e+04
        20     -134.8      886.5
        40     -54.53      205.2
        60     -47.54      122.9
        80     -44.58      55.14
       100     -43.89      40.53
0.408715141106 0.999159310635 0.97141656158
0.12742749857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.033e+04
        20     -108.2  1.085e+03
        40     -48.89      244.0
        60     -39.43      91.91
        80      -37.7      33.26
       100     -37.26      19.28
0.408715141106 1.0 0.974358974359
0.12742749857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.053e+04
        20     -120.3      798.0
        40     -47.82      142.3
        60     -43.03      90.12
        80     -41.71      27.67
       100     -41.47      19.55
0.408715141106 0.999159310635 0.972257250946
0.12742749857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.084e+04
        20     -131.1  1.057e+03
        40     -54.39      243.1
        60     -43.25      94.99
        80     -41.62      183.1
       100     -40.65      34.41
0.408715141106 1.0 0.97604035309
0.162377673919
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.062e+04
        20     -136.7      907.1
        40      -58.2      174.8
        60     -51.24      75.85
        80     -50.23      35.04
       100     -49.86      24.62
0.454083723835 0.998738965952 0.973518284994
0.162377673919
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.033e+04
        20     -109.1  1.047e+03
        40     -53.47      179.3
        60     -46.03      93.89
        80     -43.83      60.06
       100      -43.4      26.24
0.454083723835 0.999579655317 0.974779319042
0.162377673919
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.053e+04
        20     -125.5      798.7
        40     -56.31      229.3
        60      -48.9      60.03
        80     -48.05      33.32
       100     -47.79      20.71
0.454083723835 0.999159310635 0.971836906263
0.162377673919
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.084e+04
        20     -132.9   1.07e+03
        40     -60.08      239.5
        60     -52.54      308.7
        80     -88.41  3.217e+03
       100     -47.04      40.55
0.454083723835 1.0 0.975620008407
0.162377673919
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.057e+04
        20     -96.71      640.7
        40     -49.54      130.4
        60     -46.68      56.18
        80     -45.93       36.6
       100     -45.61      23.14
0.454083723835 0.999159310635 0.975620008407
0.206913808111
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.033e+04
        20     -109.9      970.8
        40      -57.9      184.5
        60      -51.1      62.62
        80     -50.78      289.3
       100     -49.96      10.58
0.504488352679 0.999159310635 0.973938629676
0.206913808111
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.053e+04
        20     -128.9      813.0
        40      -61.1      165.4
        60     -56.42      75.22
        80     -54.95      23.82
       100      -54.8      25.66
0.504488352679 0.999159310635 0.971836906263
0.206913808111
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.084e+04
        20     -135.0  1.083e+03
        40     -72.78      334.2
        60     -57.68      170.5
        80     -55.31      59.96
       100     -54.58      60.48
0.504488352679 0.999579655317 0.976460697772
0.206913808111
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.057e+04
        20     -99.71      636.2
        40     -54.54      86.95
        60     -52.82       39.1
        80      -52.4      19.46
       100     -52.28      12.89
0.504488352679 0.999159310635 0.973938629676
0.206913808111
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.04e+04
        20     -126.5      913.2
        40     -71.65      286.4
        60     -59.66      158.5
        80     -57.28      64.44
       100     -56.81      237.4
0.504488352679 0.999159310635 0.968894493485
0.263665089873
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.053e+04
        20     -131.2      863.9
        40     -69.56      184.3
        60     -132.9  3.004e+03
        80     -63.19      38.77
       100     -64.16      414.0
0.560488043569 0.998738965952 0.973518284994
0.263665089873
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.084e+04
        20     -137.4  1.097e+03
        40     -71.76      265.6
        60     -64.25      117.3
        80     -63.18      56.85
       100     -62.72      35.21
0.560488043569 0.999579655317 0.97604035309
0.263665089873
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.057e+04
        20     -102.9      620.1
        40     -74.51  1.115e+03
        60     -61.52      65.99
        80     -60.67      46.36
       100     -60.38      26.59
0.560488043569 0.999159310635 0.975199663724
0.263665089873
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.04e+04
        20     -132.7      981.6
        40     -78.09      254.6
        60     -70.11      350.6
        80     -65.82      84.06
       100     -64.61      46.84
0.560488043569 0.998738965952 0.969314838167
0.263665089873
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.089e+04
        20     -113.1      839.4
        40     -60.23      169.4
        60     -56.57      87.21
        80     -55.47      36.75
       100     -55.16      26.69
0.560488043569 0.999159310635 0.972677595628
0.335981828628
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.084e+04
        20     -139.8  1.107e+03
        40     -81.84      322.8
        60     -74.59      99.96
        80     -72.65       60.3
       100     -72.12      37.74
0.622703864848 0.999579655317 0.976460697772
0.335981828628
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.057e+04
        20     -106.8      598.1
        40     -74.22      231.0
        60     -70.11      67.53
        80     -69.19      30.12
       100     -68.99      17.92
0.622703864848 0.999159310635 0.975620008407
0.335981828628
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.04e+04
        20     -128.1      756.6
        40      -92.6      500.5
        60     -80.85      207.0
        80     -76.49      141.3
       100     -74.15      61.28
0.622703864848 0.998738965952 0.96973518285
0.335981828628
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.089e+04
        20     -114.1  1.031e+03
        40     -70.05      216.4
        60     -66.73      340.1
        80     -63.67      38.93
       100     -63.35      21.13
0.622703864848 0.999159310635 0.972677595628
0.335981828628
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.03e+04
        20     -146.2      989.8
        40     -83.09      324.7
        60     -95.22      561.1
        80     -72.39      213.8
       100     -70.58      46.03
0.622703864848 0.999579655317 0.972257250946
0.428133239872
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.057e+04
        20     -111.4      589.8
        40     -82.96      242.5
        60     -80.12      86.23
        80     -79.24      68.88
       100      -79.0      39.79
0.691825825271 0.997477931904 0.975199663724
0.428133239872
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.04e+04
        20     -123.5      658.6
        40      -91.1      245.7
        60     -88.91      709.7
        80     -84.89      89.13
       100     -84.15      42.71
0.691825825271 0.997898276587 0.969314838167
0.428133239872
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.089e+04
        20     -117.1      662.4
        40     -79.27      785.8
        60     -73.76      96.95
        80     -72.94      79.68
       100     -72.44      17.05
0.691825825271 0.998738965952 0.971836906263
0.428133239872
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.03e+04
        20     -144.9      850.6
        40     -93.49      963.5
        60     -81.82      303.9
        80     -80.52      49.85
       100     -80.14      47.35
0.691825825271 0.999579655317 0.97141656158
0.428133239872
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.147e+04
        20     -121.9      666.1
        40      -86.6      157.3
        60     -83.76      67.66
        80     -83.19      25.04
       100     -83.32      101.2
0.691825825271 0.999159310635 0.973097940311
0.545559478117
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.04e+04
        20     -124.3      527.4
        40     -98.14      136.3
        60     -96.09      150.5
        80     -95.16      29.21
       100     -95.09      15.71
0.768620526594 0.997898276587 0.969314838167
0.545559478117
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.089e+04
        20     -119.0      754.4
        40     -89.29      249.1
        60     -84.12      151.4
        80     -83.23      80.53
       100     -82.77      32.06
0.768620526594 0.998318621269 0.972677595628
0.545559478117
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.03e+04
        20     -151.4      905.7
        40     -97.44      551.1
        60     -92.63      106.5
        80     -91.72      71.05
       100     -91.21      66.97
0.768620526594 0.997057587222 0.970996216898
0.545559478117
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.147e+04
        20     -129.8      640.0
        40     -195.0  4.801e+03
        60     -95.74      91.78
        80     -95.05      50.67
       100     -94.78      22.83
0.768620526594 0.997477931904 0.973097940311
0.545559478117
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.098e+04
        20     -142.6      753.7
        40     -104.9      277.7
        60     -97.52      158.2
        80     -95.57      87.37
       100     -94.88      47.69
0.768620526594 0.997898276587 0.975199663724
0.695192796178
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.089e+04
        20     -139.6      809.9
        40      -95.1      117.7
        60     -94.12      44.11
        80     -93.87      48.61
       100     -93.74      17.59
0.853939665624 0.997477931904 0.971836906263
0.695192796178
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.03e+04
        20     -154.7      909.6
        40     -112.4      346.2
        60     -104.2      97.96
        80     -103.2      45.28
       100     -102.9      36.14
0.853939665624 0.996637242539 0.972257250946
0.695192796178
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.147e+04
        20     -137.3      645.9
        40     -110.8      186.9
        60     -108.2      72.47
        80     -107.4      40.59
       100     -107.3      33.57
0.853939665624 0.997057587222 0.973938629676
0.695192796178
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.098e+04
        20     -152.5      744.4
        40     -111.1      201.1
        60     -108.0      95.87
        80     -107.4      35.59
       100     -107.2      42.65
0.853939665624 0.997477931904 0.97604035309
0.695192796178
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.073e+04
        20     -147.8      597.5
        40     -172.3  3.062e+03
        60     -106.9      107.8
        80     -105.7      120.5
       100     -105.2      48.99
0.853939665624 0.997898276587 0.97604035309
0.88586679041
Iteration  Log-likelihood |gradient|
         0 -1.649e+03   2.03e+04
        20     -148.6      857.1
        40     -119.3      198.8
        60     -116.3      86.41
        80     -115.8       49.0
       100     -115.6      54.05
0.948729480016 0.996216897856 0.97141656158
0.88586679041
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.147e+04
        20     -211.5   3.05e+03
        40     -122.0      104.5
        60     -121.0      39.54
        80     -120.8      19.43
       100     -120.8      7.378
0.948729480016 0.995376208491 0.973938629676
0.88586679041
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.098e+04
        20     -164.6      793.2
        40     -126.7      308.9
        60     -122.9      211.3
        80     -121.3      114.3
       100     -121.0      37.25
0.948729480016 0.996637242539 0.976460697772
0.88586679041
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.073e+04
        20     -152.0      656.9
        40     -125.7      349.2
        60     -119.5      102.4
        80     -136.9  2.546e+03
       100     -118.0      26.47
0.948729480016 0.996216897856 0.974779319042
0.88586679041
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.219e+04
        20     -266.8  3.843e+03
        40     -130.5      522.1
        60     -121.4      189.4
        80     -119.4      128.9
       100     -118.9      160.4
0.948729480016 0.994955863808 0.971836906263
1.12883789168
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.147e+04
        20     -153.9      691.4
        40     -136.9      145.7
        60     -135.6      84.63
        80     -135.4      38.42
       100     -135.2       29.1
1.05404124259 0.994115174443 0.974358974359
1.12883789168
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.098e+04
        20     -174.3      857.1
        40     -142.4      440.7
        60     -136.9      148.8
        80     -135.6      106.6
       100     -135.5      33.05
1.05404124259 0.994535519126 0.976460697772
1.12883789168
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.073e+04
        20     -889.8  2.349e+04
        40     -138.3      249.8
        60     -134.2      182.9
        80     -132.7      76.48
       100     -132.2      46.97
1.05404124259 0.994535519126 0.975199663724
1.12883789168
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.219e+04
        20 -2.927e+03  3.518e+04
        40     -138.9      330.2
        60     -133.0      178.6
        80     -132.6      55.28
       100     -132.3      138.4
1.05404124259 0.993274485078 0.971836906263
1.12883789168
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.129e+04
        20     -134.5      402.7
        40     -124.4      136.5
        60     -123.6      73.86
        80     -123.3      35.03
       100     -123.2      17.82
1.05404124259 0.995376208491 0.970155527533
1.43844988829
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.098e+04
        20     -184.5      847.3
        40     -154.1      218.9
        60     -151.0       76.1
        80     -150.7      27.75
       100     -150.7      8.985
1.17104292054 0.992433795712 0.976881042455
1.43844988829
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.073e+04
        20     -208.1  1.705e+03
        40     -149.8      199.3
        60     -147.6      67.84
        80     -147.3      61.21
       100     -147.1      30.73
1.17104292054 0.99369482976 0.975199663724
1.43844988829
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.219e+04
        20 -2.434e+03  3.117e+04
        40     -153.4      419.0
        60     -147.7      160.9
        80     -147.1      57.44
       100     -146.9      31.47
1.17104292054 0.990332072299 0.972257250946
1.43844988829
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.129e+04
        20     -147.2      437.5
        40     -138.0      148.5
        60     -137.1      53.81
        80     -136.9      16.89
       100     -136.8      5.526
1.17104292054 0.99201345103 0.96973518285
1.43844988829
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.029e+04
        20     -180.6      938.8
        40     -148.3      192.3
        60     -147.0      187.3
        80     -146.6      20.09
       100     -146.5      11.36
1.17104292054 0.992433795712 0.972677595628
1.83298071083
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.073e+04
        20     -211.6  1.377e+03
        40     -164.0      208.7
        60     -162.6      47.37
        80     -162.5      32.84
       100     -162.4      21.11
1.30103212886 0.992854140395 0.974779319042
1.83298071083
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.219e+04
        20 -2.029e+03  2.927e+04
        40     -167.1      327.4
        60     -162.5      122.4
        80     -162.1      76.15
       100     -161.9       18.9
1.30103212886 0.988230348886 0.971836906263
1.83298071083
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.129e+04
        20     -161.1      414.5
        40     -152.2      139.2
        60     -151.5      59.81
        80     -151.3      29.93
       100     -151.2      13.14
1.30103212886 0.990752416982 0.96973518285
1.83298071083
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.029e+04
        20     -185.7      790.6
        40     -162.7      164.8
        60     -161.6      41.03
        80     -161.5      12.63
       100     -161.5      6.598
1.30103212886 0.989911727617 0.972257250946
1.83298071083
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.083e+04
        20     -164.7      496.5
        40     -153.2      132.6
        60     -152.7      44.25
        80     -152.5      24.39
       100     -152.5      9.843
1.30103212886 0.993274485078 0.973938629676
2.33572146909
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.219e+04
        20 -2.368e+03  3.451e+04
        40     -180.1      220.1
        60     -177.6       69.1
        80     -177.5       22.2
       100     -177.4      12.65
1.44545052161 0.987389659521 0.970575872215
2.33572146909
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.129e+04
        20     -173.1      431.2
        40     -166.8      86.67
        60     -166.5      41.81
        80     -166.4       25.2
       100     -166.3      10.86
1.44545052161 0.988230348886 0.969314838167
2.33572146909
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.029e+04
        20     -201.8  1.093e+03
        40     -179.8      918.3
        60     -177.0      33.17
        80     -176.9      9.019
       100     -176.9      2.884
1.44545052161 0.987389659521 0.97141656158
2.33572146909
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.083e+04
        20     -178.9      483.5
        40     -168.4      79.79
        60     -168.1      35.37
        80     -168.0      23.09
       100     -168.0      3.171
1.44545052161 0.991172761665 0.972677595628
2.33572146909
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.032e+04
        20     -186.7  1.317e+03
        40     -166.4      235.9
        60     -164.2       93.9
        80     -164.0      34.35
       100     -164.0      23.24
1.44545052161 0.99201345103 0.972677595628
2.97635144163
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.129e+04
        20     -187.8      531.6
        40     -182.6      257.6
        60     -182.1      29.85
        80     -182.0      11.06
       100     -182.0      21.52
1.6058997807 0.986128625473 0.970155527533
2.97635144163
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.029e+04
        20     -212.4  1.117e+03
        40     -193.5      151.2
        60     -192.8      24.67
        80     -192.8      7.213
       100     -192.8      5.445
1.6058997807 0.98570828079 0.97141656158
2.97635144163
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.083e+04
        20     -194.5      554.2
        40     -184.3      48.25
        60     -184.3      166.3
        80     -184.2      6.067
       100     -184.2      2.355
1.6058997807 0.989911727617 0.971836906263
2.97635144163
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.032e+04
        20     -203.3  1.136e+03
        40     -187.6  1.685e+03
        60     -181.4      44.41
        80     -181.3      17.13
       100     -181.3      8.705
1.6058997807 0.990332072299 0.973097940311
2.97635144163
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.137e+04
        20     -180.0      192.2
        40     -178.7      11.65
        60     -178.7      2.019
        80     -178.7     0.2758
1.6058997807 0.987810004203 0.970575872215
3.79269019073
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.029e+04
        20     -220.5      937.9
        40     -209.7      230.8
        60     -209.1      44.92
        80     -209.1      8.977
       100     -209.1      2.155
1.78415937944 0.984447246742 0.971836906263
3.79269019073
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.083e+04
        20     -209.2      560.1
        40     -200.8      45.66
        60     -200.8      7.618
        80     -200.8      2.399
       100     -200.8     0.9927
1.78415937944 0.987810004203 0.96973518285
3.79269019073
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.032e+04
        20     -232.5  1.466e+03
        40     -199.8      120.3
        60     -199.4      47.56
        80     -199.5      215.8
       100     -199.3      4.267
1.78415937944 0.987810004203 0.971836906263
3.79269019073
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.137e+04
        20     -195.3      153.6
        40     -194.6      13.09
        60     -194.6      7.082
        80     -194.6      1.322
1.78415937944 0.985287936108 0.970575872215
3.79269019073
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.034e+04
        20     -233.7  1.121e+03
        40     -212.6      165.3
        60     -212.1      81.37
        80     -212.0      14.45
       100     -212.0      4.175
1.78415937944 0.98402690206 0.970996216898
4.83293023857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.083e+04
        20     -231.7  1.257e+03
        40     -218.1      57.64
        60     -218.0      17.77
        80     -218.0      1.602
1.98220631793 0.984867591425 0.969314838167
4.83293023857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.032e+04
        20     -227.6      679.5
        40     -218.4      94.18
        60     -218.0      28.42
        80     -217.9       3.51
       100     -217.9      0.989
1.98220631793 0.985287936108 0.97141656158
4.83293023857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.137e+04
        20     -211.8      138.6
        40     -211.1      26.66
        60     -211.1      5.762
        80     -211.1     0.5443
1.98220631793 0.982765868012 0.968894493485
4.83293023857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.034e+04
        20     -243.8      874.3
        40     -230.1      108.7
        60     -229.7      57.19
        80     -229.6      17.26
       100     -229.6      3.144
1.98220631793 0.980664144599 0.970575872215
4.83293023857
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.117e+04
        20     -226.0      205.3
        40     -224.4      31.87
        60     -224.4       6.37
        80     -224.4      3.814
1.98220631793 0.981925178646 0.973097940311
6.15848211066
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.032e+04
        20     -245.2      769.5
        40     -237.6      61.39
        60     -237.3      32.41
        80     -237.3      5.189
       100     -237.3      1.518
2.20223704905 0.983186212694 0.97141656158
6.15848211066
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.137e+04
        20     -228.9      134.8
        40     -228.3      29.96
        60     -228.3       1.95
2.20223704905 0.979823455233 0.968053804119
6.15848211066
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.034e+04
        20     -254.7      544.2
        40     -248.2      88.84
        60     -247.9       41.7
        80     -247.8      19.93
       100     -247.8      5.935
2.20223704905 0.979403110551 0.970575872215
6.15848211066
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.117e+04
        20     -242.5      236.3
        40     -240.9      75.07
        60     -240.9      4.693
        80     -240.9      1.144
       100     -240.9     0.6305
2.20223704905 0.978562421185 0.972677595628
6.15848211066
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.112e+04
        20     -244.2      466.5
        40     -240.6      223.4
        60     -240.5      2.361
2.20223704905 0.980664144599 0.974358974359
7.84759970351
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.137e+04
        20     -247.1      139.4
        40     -246.5      49.23
        60     -246.5      18.42
        80     -246.5      1.369
2.44669183846 0.97772173182 0.966792770071
7.84759970351
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.034e+04
        20     -274.2      637.9
        40     -267.2      113.0
        60     -267.0      57.87
        80     -266.9      6.465
       100     -266.9      9.352
2.44669183846 0.978982765868 0.970155527533
7.84759970351
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.117e+04
        20     -260.0      247.6
        40     -259.1      651.5
        60     -258.2      16.43
        80     -258.2      1.096
2.44669183846 0.977301387137 0.970996216898
7.84759970351
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.112e+04
        20     -261.3      288.8
        40     -259.2      29.57
        60     -259.2      2.661
2.44669183846 0.97772173182 0.973097940311
7.84759970351
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.126e+04
        20     -279.5      281.6
        40     -277.5      76.58
        60     -277.5        9.0
        80     -277.5      3.706
2.44669183846 0.977301387137 0.973518284994
10.0
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.034e+04
        20     -299.2      513.8
        40     -287.2      293.3
        60     -287.0      11.35
        80     -287.0      5.437
2.71828182846 0.975199663724 0.968894493485
10.0
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.117e+04
        20     -277.7      271.1
        40     -276.5      62.28
        60     -276.5      6.852
        80     -276.5      6.781
2.71828182846 0.974779319042 0.969314838167
10.0
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.112e+04
        20     -279.7      196.7
        40     -279.0      25.05
        60     -279.0      2.653
2.71828182846 0.975620008407 0.970575872215
10.0
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.126e+04
        20     -299.7      299.2
        40     -298.0      636.6
        60     -297.4      8.921
        80     -297.4      4.051
2.71828182846 0.974779319042 0.971836906263
10.0
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  2.092e+04
        20     -260.6      120.8
        40     -260.2      26.37
        60     -260.2      3.765
2.71828182846 0.978562421185 0.962589323245

In [82]:
plt.xscale('log')
plt.scatter([10.0**np.log(i) for i in regs], [acc for acc in accs_val], marker='x', label='Validation set')
plt.scatter([10.0**np.log(i) for i in regs], [acc for acc in accs_train], c='r', marker='x', label='Training set')
plt.legend()
plt.title('HACRF with transition features')
plt.xlabel('Regularisation')
plt.ylabel('Accuracy')
plt.xlim(0.08, 11)


Out[82]:
(0.08, 11)

In [86]:
import cPickle  # Lets store the results in case we need them later
cPickle.dump((models, accs_train, accs_val, regs), open('models/val_for_reg_hacrf_t.pkl', 'wb'))

I initially thought that 1 is a good enough value to continue with, but eventually went with 10 because the transition weight pictures looked less noisy and more interesting.

Train model with this regularisation


In [102]:
m = Hacrf(l2_regularization=10.0, optimizer=fmin_l_bfgs_b, optimizer_kwargs={'maxfun': 45}, state_machine=None)
m.fit(x_train, y_train, verbosity=20)


Iteration  Log-likelihood |gradient|
         0 -3.298e+03   4.01e+04
        20     -541.5  2.892e+03
        40     -489.6      312.3
Out[102]:
<pyhacrf.pyhacrf.Hacrf at 0x293d92510>

Evaluate


In [103]:
from sklearn.metrics import accuracy_score

In [104]:
from sklearn.metrics import confusion_matrix
pr = m.predict(x_train)
print confusion_matrix(y_train, pr)
print '{:.2}% error'.format((1 - accuracy_score(y_train, pr)) * 100)


[[2306   58]
 [  47 2347]]
2.2% error

In [105]:
pr = m.predict(x_test)
print confusion_matrix(y_test, pr)
print '{:.2}% error'.format((1 - accuracy_score(y_test, pr)) * 100)


[[599  11]
 [ 19 561]]
2.5% error

The error on the test set is higher than that on the training set. This means that the model is probably still overfitting.

Visualise transitions

Let's try to visualise the learned transition matrix. There are many of these matrices, one per edit transition and per class. So for this model, there are 3 x 2 = 6. Let's just look at the matching class' substitute edit transition.


In [106]:
plt.figure(figsize=(8, 8))
plt.imshow(m.parameters[0, 3:].reshape(63, 63)[:27, :27], interpolation='nearest', vmin=-0.8, vmax=0.8, cmap='seismic')
plt.xticks(range(27), fe.CHARACTERS[:27])
plt.yticks(range(27), fe.CHARACTERS[:27])
plt.title('Weights learned for the matching states')
plt.colorbar()
print m.parameters[0, :3]


[-0.62857721  1.86023252  0.        ]

In [107]:
plt.figure(figsize=(8, 8))
plt.imshow(m.parameters[1, 3:].reshape(63, 63)[:27, :27], interpolation='nearest', vmin=-0.8, vmax=0.8, cmap='seismic')
plt.xticks(range(27), fe.CHARACTERS[:27])
plt.yticks(range(27), fe.CHARACTERS[:27])
plt.title('Weights learned for the non-matching states')
plt.colorbar()


Out[107]:
<matplotlib.colorbar.Colorbar instance at 0x284b0b3b0>

Also train baseline model

Now repeat the above steps for a model without the transition features so we can compare results.


In [182]:
fe_base = StringPairFeatureExtractor(match=True, numeric=True)
x_orig_base = fe_base.fit_transform(x_raw)
x_train_base, x_test_base, y_train_base, y_test_base = train_test_split(x_orig_base, y_orig, test_size=0.2, random_state=42)
models_base = []
accs_train_base = []
accs_val_base = []
regs_base = []
for n, i in enumerate(np.linspace(-10, 4, 25)):
    print np.exp(i)
    m_base = Hacrf(l2_regularization=np.exp(i), optimizer=fmin_l_bfgs_b, optimizer_kwargs={'maxfun': 45})
    
    x_t, x_v, y_t, y_v = train_test_split(x_train_base, y_train, test_size=0.5, random_state=42 + n)
    
    m_base.fit(x_t, y_t, verbosity=5)
    
    train_score = accuracy_score(m_base.predict(x_t), y_t)
    val_score = accuracy_score(m_base.predict(x_v), y_v)
    print np.exp(i), train_score, val_score
    
    regs_base.append(np.exp(i))
    models_base.append(m_base)
    accs_train_base.append(train_score)
    accs_val_base.append(val_score)


4.53999297625e-05
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.727e+03
         5     -491.9   1.18e+03
        10     -340.0      386.5
        15     -307.2      535.0
        20     -259.0      48.86
        25     -475.1  7.656e+03
        30     -251.3      103.3
        35     -248.2       17.5
        40     -247.6      41.99
        45     -247.0      21.01
4.53999297625e-05 0.966792770071 0.966372425389
8.1356757019e-05
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.169e+03
         5     -523.5  1.441e+03
        10     -330.2      213.2
        15     -287.7      345.9
        20     -264.8      180.4
        25     -256.1      46.06
        30     -258.2      576.8
        35     -253.3      43.17
        40     -250.1      46.58
        45     -249.8       16.9
8.1356757019e-05 0.965531736024 0.965111391341
0.000145791457108
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.791e+03
         5     -482.2   1.05e+03
        10     -353.2      485.7
        15     -304.9      551.1
        20     -246.0      96.96
        25     -236.5      30.26
        30     -233.1      370.0
        35     -231.7      35.96
        40     -231.0      33.88
        45     -229.5      89.23
0.000145791457108 0.967213114754 0.964691046658
0.000261258557302
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.981e+03
         5     -513.3  1.204e+03
        10     -339.2      187.6
        15     -301.8      238.4
        20     -300.5      738.1
        25     -258.0      188.8
        30     -253.6      46.34
        35     -252.0      34.47
        40     -251.6      83.34
        45     -250.6      135.6
0.000261258557302 0.965952080706 0.966792770071
0.000468175811653
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.163e+03
         5     -456.3  1.131e+03
        10     -296.3      152.1
        15     -269.0      276.3
        20     -222.9      377.3
        25     -219.6      805.6
        30     -214.0      17.35
        35     -212.2      12.17
        40     -212.2      8.349
        45     -211.9      11.64
0.000468175811653 0.96973518285 0.963850357293
0.00083897190921
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.866e+03
         5     -434.7      975.7
        10     -355.7      276.1
        15     -316.2      732.3
        20     -271.2      191.4
        25     -267.7       70.2
        30     -265.4      46.88
        35     -263.5      81.06
        40     -262.4      29.67
        45     -260.4      125.9
0.00083897190921 0.965111391341 0.968053804119
0.00150343919298
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.848e+03
         5     -380.2      763.0
        10     -339.6      452.9
        15     -303.8      244.9
        20     -263.6      642.2
        25     -252.0      103.5
        30     -250.6      77.54
        35     -247.3      60.14
        40     -246.2      20.97
        45     -245.4      104.3
0.00150343919298 0.966372425389 0.966372425389
0.00269416577858
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.714e+03
         5     -394.6      768.5
        10     -354.7      366.3
        15     -285.7      471.9
        20     -266.0      632.7
        25     -259.7      225.6
        30     -257.4      244.8
        35     -256.5      222.4
        40     -254.5      278.1
        45     -252.6      55.66
0.00269416577858 0.964691046658 0.965952080706
0.00482794999383
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.731e+03
         5     -440.6  1.004e+03
        10     -365.4      334.8
        15     -369.1   3.93e+03
        20     -264.5      106.4
        25     -259.0      80.23
        30     -257.0      81.79
        35     -255.6      27.07
        40     -260.0      283.8
        45     -260.3      782.6
0.00482794999383 0.965111391341 0.960907944515
0.00865169520312
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.825e+03
         5     -505.5  1.111e+03
        10     -370.3      259.0
        15     -323.9      352.7
        20     -266.2  1.103e+03
        25     -243.0      314.2
        30     -240.0      124.3
        35     -237.2      39.74
        40     -236.1      53.77
        45     -235.3      137.1
0.00865169520312 0.967633459437 0.957124842371
0.015503853599
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.421e+03
         5     -428.1      710.3
        10     -320.8      286.5
        15     -583.8  9.002e+03
        20     -249.2      31.92
        25     -247.3      165.3
        30     -245.8      43.06
        35     -243.7      72.74
        40     -241.9      57.35
        45     -241.2      29.11
0.015503853599 0.96973518285 0.965111391341
0.0277829339541
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.269e+03
         5     -532.6  1.329e+03
        10     -365.3      358.4
        15     -365.3  3.297e+03
        20     -295.0      348.5
        25     -283.3      299.7
        30     -278.4      94.02
        35     -275.8      121.2
        40     -274.9      31.62
        45     -271.8       97.7
0.0277829339541 0.965111391341 0.96973518285
0.0497870683679
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.033e+03
         5     -534.7  1.126e+03
        10     -394.3      249.7
        15     -358.0      436.0
        20     -289.4      310.4
        25     -277.5      59.46
        30     -268.9      81.45
        35     -265.0      104.7
        40     -263.6      39.24
        45     -262.8      23.89
0.0497870683679 0.963009667928 0.966792770071
0.0892185174093
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.739e+03
         5     -437.5  1.132e+03
        10     -340.8      352.6
        15     -307.1      306.9
        20     -261.4      63.26
        25     -255.9      98.17
        30     -276.9  2.073e+03
        35     -252.5      141.3
        40     -251.4      75.56
        45     -248.9      34.53
0.0892185174093 0.969314838167 0.96174863388
0.15987974608
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.219e+03
         5     -420.2      439.0
        10     -314.8      542.5
        15     -282.6      305.9
        20     -270.7      161.5
        25     -267.0      41.39
        30     -265.9       85.4
        35     -262.7       29.1
        40     -259.3      70.85
        45     -257.8      96.83
0.15987974608 0.967213114754 0.967213114754
0.28650479686
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.678e+03
         5     -488.1  1.185e+03
        10     -317.6      264.5
        15     -281.3      180.1
        20     -244.4      78.28
        25     -239.3      100.6
        30     -237.0      18.77
        35     -256.9  1.734e+03
        40     -236.3      12.83
        45     -236.1      71.61
0.28650479686 0.968894493485 0.964270701976
0.513417119033
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.777e+03
         5     -486.6  1.165e+03
        10     -338.6      274.3
        15     -316.5      312.9
        20     -270.6      38.89
        25     -267.8      23.43
        30     -267.3      160.7
        35     -266.5      12.79
        40     -266.4       11.4
        45     -266.0      14.15
0.513417119033 0.966792770071 0.966372425389
0.920044414629
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.743e+03
         5     -450.6  1.177e+03
        10     -384.9      439.4
        15     -333.3      187.8
        20     -307.3      266.5
        25     -286.4      104.4
        30     -283.1      210.0
        35     -278.7      55.94
        40     -273.7      501.4
        45     -265.7      225.4
0.920044414629 0.968894493485 0.960067255149
1.6487212707
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.402e+03
         5     -385.4  1.066e+03
        10     -327.4      311.1
        15     -284.7      175.1
        20     -264.5      82.16
        25     -263.1      199.5
        30     -261.3      47.09
        35     -260.5      134.8
        40     -259.9      19.48
        45     -259.7      20.54
1.6487212707 0.968474148802 0.962589323245
2.95451152709
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.682e+03
         5     -484.8      888.0
        10     -381.1      440.1
        15     -340.0      730.8
        20     -309.8      266.7
        25     -300.1      56.26
        30     -299.6      70.86
        35     -296.7      76.77
        40     -296.3      106.9
        45     -295.9      49.69
2.95451152709 0.963850357293 0.961328289197
5.29449005047
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.267e+03
         5     -421.4      625.1
        10     -343.3      159.8
        15     -317.4      424.7
        20     -312.3      145.6
        25     -310.0      24.56
        30     -309.0      21.24
        35     -308.8      47.64
        40     -308.6       87.7
        45     -308.5      11.32
5.29449005047 0.965111391341 0.960907944515
9.48773583636
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.344e+03
         5     -429.7      738.0
        10     -373.0      511.6
        15     -356.2      118.7
        20     -353.7      143.9
        25     -352.2      127.9
        30     -351.2      229.5
        35     -350.7      41.64
        40     -350.5      8.913
        45     -350.5      38.77
9.48773583636 0.963850357293 0.960067255149
17.0020399401
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.326e+03
         5     -482.3      489.3
        10     -425.8      695.0
        15     -418.4  1.206e+03
        20     -413.2      61.13
        25     -411.5      53.52
        30     -411.0      40.48
        35     -410.9      40.17
        40     -410.6      50.46
        45     -410.5      24.68
17.0020399401 0.956284153005 0.960487599832
30.4676866125
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  6.153e+03
         5     -514.2      791.8
        10     -449.9      484.5
        15     -454.5  1.859e+03
        20     -442.0      42.89
        25     -441.5      18.82
        30     -441.5      9.878
        35     -441.5     0.5749
        40     -441.5     0.1629
        45     -441.5      0.062
30.4676866125 0.957545187053 0.95376208491
54.5981500331
Iteration  Log-likelihood |gradient|
         0 -1.649e+03  5.956e+03
         5     -564.5  1.113e+03
        10     -495.0      369.9
        15     -964.2  7.932e+03
        20     -476.0      194.7
        25     -474.8      138.1
        30     -474.5      42.05
        35     -474.3      23.01
        40     -474.2      4.242
        45     -474.2     0.9528
54.5981500331 0.957124842371 0.948717948718

In [197]:
plt.xscale('log')
plt.scatter([(i) for i in regs_base], [acc for acc in accs_val_base], marker='x', label='Validation set')
plt.scatter([(i) for i in regs_base], [acc for acc in accs_train_base], c='r', marker='x', label='Training set')
plt.legend()
plt.xlabel('Regularisation parameter')
plt.ylabel('Accuracy')


Out[197]:
<matplotlib.text.Text at 0x27dfcdfd0>

In [185]:
m_base = Hacrf(l2_regularization=0.1, optimizer=fmin_l_bfgs_b, optimizer_kwargs={'maxfun': 25}, state_machine=None)
m_base.fit(x_train_base, y_train, verbosity=5)
pr = m_base.predict(x_train_base)
print 'Training score:'
print confusion_matrix(y_train, pr)
print '{:.2}% error'.format((1 - accuracy_score(y_train, pr)) * 100)
print 'Testing score:'
pr = m_base.predict(x_test_base)
print confusion_matrix(y_test, pr)
print '{:.2}% error'.format((1 - accuracy_score(y_test, pr)) * 100)


Iteration  Log-likelihood |gradient|
         0 -3.298e+03  1.163e+04
         5     -842.8  2.713e+03
        10     -735.7      988.8
        15     -651.0      779.4
        20     -532.3      253.4
        25     -525.6      90.15
Training score:
[[2273   91]
 [  76 2318]]
3.5% error
Testing score:
[[594  16]
 [ 10 570]]
2.2% error

Generate example candidate corrections

Now, let's take the 1000 pairs we held out at the start, and try to recover the correct token from a list of dictionary words given the incorrect one.

To do this, we'll construct a 1000 new sets of pairs. For each incorrect token, we'll construct a list of pairs where the first element in the pair is the incorrect token, and the second element is a candidate correct token from a dictionary.


In [33]:
dictionary = [line.split()[1].strip() for line in open('uk_word_freq.txt', 'r').readlines()[1:]]
print len(dictionary)
dictionary[:10]


67590
Out[33]:
['the', 'of', 'and', 'a', 'in', 'to', 'it', 'is', 'was', 'to']

In [34]:
for incorrect, correct in ppairs_test[:10]:
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:10000] + [correct])]
    gx_test = fe.transform(test_pairs)
    pr = m.predict_proba(gx_test)
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: -x[0][0])
    print (incorrect, correct),
    print [(candidate[1][1], '{:.3f}'.format(candidate[0][0])) for candidate in cr[:10]]
    print


('jokies', 'jokes') [('jokes', '0.997'), ('jobs', '0.984'), ('jones', '0.964'), ('joke', '0.913'), ('jenkins', '0.869'), ('jews', '0.867'), ('jesus', '0.822'), ('joe', '0.811'), ('james', '0.803'), ('joy', '0.797')]

('payed', 'paid') [('played', '0.999'), ('paused', '0.998'), ('passed', '0.998'), ('paid', '0.997'), ('packed', '0.994'), ('persuaded', '0.993'), ('poured', '0.993'), ('prepared', '0.993'), ('pushed', '0.992'), ('proved', '0.992')]

('comming', 'coming') [('coming', '1.000'), ('combining', '1.000'), ('comment', '0.998'), ('command', '0.998'), ('common', '0.998'), ('commitment', '0.997'), ('commit', '0.997'), ('commons', '0.997'), ('competing', '0.996'), ('comparing', '0.996')]

('joyyyyyy', 'joy') [('y', '1.000'), ('joy', '1.000'), ('july', '0.999'), ('jury', '0.999'), ('by', '0.998'), ('boy', '0.998'), ('johnny', '0.995'), ('roy', '0.995'), ('jenny', '0.994'), ('guy', '0.993')]

('growin', 'growing') [('grown', '1.000'), ('grin', '0.998'), ('growing', '0.998'), ('grain', '0.996'), ('grow', '0.984'), ('brown', '0.979'), ('gordon', '0.978'), ('grows', '0.978'), ('growth', '0.972'), ('gon', '0.963')]

('botherin', 'bothering') [('bothering', '0.999'), ('bother', '0.987'), ('born', '0.983'), ('bothered', '0.961'), ('brother', '0.952'), ('brothers', '0.934'), ('burn', '0.919'), ('northern', '0.907'), ('barn', '0.902'), ('berlin', '0.871')]

('konvict', 'convict') [('convict', '0.997'), ('kent', '0.901'), ('conflict', '0.879'), ('kit', '0.851'), ('contact', '0.838'), ('convert', '0.795'), ('convenient', '0.763'), ('soviet', '0.725'), ('contract', '0.715'), ('conduct', '0.660')]

('da', 'the') [('da', '0.998'), ('dna', '0.997'), ('daddy', '0.996'), ('data', '0.996'), ('diana', '0.994'), ('donna', '0.991'), ('drama', '0.991'), ('day', '0.991'), ('dad', '0.989'), ('days', '0.986')]

('dickk', 'dick') [('dick', '1.000'), ('deck', '0.999'), ('dock', '0.999'), ('duck', '0.999'), ('disk', '0.999'), ('desk', '0.993'), ('dark', '0.992'), ('derek', '0.991'), ('kick', '0.987'), ('sick', '0.986')]

('steem', 'steam') [('steam', '0.999'), ('seem', '0.997'), ('stream', '0.997'), ('system', '0.997'), ('storm', '0.992'), ('item', '0.988'), ('systems', '0.980'), ('streams', '0.974'), ('stamp', '0.968'), ('steep', '0.958')]

Evaluate candidate generation

How often is the correct token in the top 1, 3, 20, or 100 candidates?

Levenshtein baseline


In [36]:
import editdistance




In [37]:
dictionary_length = len(dictionary)
dictionary_rank = dict([(word, dictionary_length - i) for i, word in enumerate(dictionary[::-1])])
dictionary_rank['a']


Out[37]:
4

In [161]:
result_file = open('levenshtein_generation_result.txt', 'a')
for i, (incorrect, correct) in enumerate(ppairs_test[:1000]):
    print i,
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:20000] + [correct])]
    pr = [editdistance.eval(*test_pair) for test_pair in test_pairs]
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: x[0])
    result_file.write('{} {} {}\n'.format(incorrect, correct,
                      [(candidate[1][1], '{:.5f}'.format(candidate[0])) for candidate in cr[:1000]]))
    result_file.flush()


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

In [161]:
result_file = open('levenshtein_generation_result_using_rank.txt', 'a')
for i, (incorrect, correct) in enumerate(ppairs_test[:1000]):
    print i,
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:20000] + [correct])]
    pr = [editdistance.eval(*test_pair) for test_pair in test_pairs]
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: (x[0], dictionary_rank.get(x[1][1], dictionary_length)))
    result_file.write('{} {} {}\n'.format(incorrect, correct,
                      [(candidate[1][1], '{:.5f}'.format(candidate[0])) for candidate in cr[:1000]]))
    result_file.flush()


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

HACRF


In [108]:
import cPickle

In [114]:
cPickle.dump(m, open('models/m_mnt.pkl', 'wb'))
cPickle.dump(ppairs_test, open('models/ppairs_test.pkl', 'wb'))
cPickle.dump(dictionary, open('models/dictionary.pkl', 'wb'))

In [79]:
result_file = open('generation_result.txt', 'a')
for i, (incorrect, correct) in enumerate(ppairs_test[:1000]):
    print i,
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:20000] + [correct])]
    gx_test = fe.transform(test_pairs)
    pr = m.predict_proba(gx_test)
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: -x[0][0])
    result_file.write('{} {} {}\n'.format(incorrect, correct,
                      [(candidate[1][1], '{:.5f}'.format(candidate[0][0])) for candidate in cr[:1000]]))
    result_file.flush()


0 1 2 3 4 5 6 7 8 9

In [186]:
result_file = open('generation_result_with_pre_rank_no_transition.txt', 'a')
for i, (incorrect, correct) in enumerate(ppairs_test[:1000]):
    print i,
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:20000] + [correct])]
    pr = [editdistance.eval(*test_pair) for test_pair in test_pairs]
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: (x[0], dictionary_rank.get(x[1][1], dictionary_length)))
    new_candidates = [candidate[1][1] for candidate in cr[:1000]]
    
    test_pairs = [(incorrect, candidate) for candidate in new_candidates]
    gx_test = fe_base.transform(test_pairs)
    pr = m_base.predict_proba(gx_test)
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: -x[0][0])
    result_file.write('{} {} {}\n'.format(incorrect, correct,
                      [(candidate[1][1], '{:.5f}'.format(candidate[0][0])) for candidate in cr[:1000]]))
    result_file.flush()


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

In [38]:
result_file = open('generation_result_with_pre_rank.txt', 'a')
for i, (incorrect, correct) in enumerate(ppairs_test[:1000]):
    print i,
    test_pairs = [(incorrect, candidate) for candidate in set(dictionary[:20000] + [correct])]
    pr = [editdistance.eval(*test_pair) for test_pair in test_pairs]
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: (x[0], dictionary_rank.get(x[1][1], dictionary_length)))
    new_candidates = [candidate[1][1] for candidate in cr[:1000]]
    
    test_pairs = [(incorrect, candidate) for candidate in new_candidates]
    gx_test = fe.transform(test_pairs)
    pr = m.predict_proba(gx_test)
    cr = zip(pr, test_pairs)
    cr = sorted(cr, key=lambda x: -x[0][0])
    result_file.write('{} {} {}\n'.format(incorrect, correct,
                      [(candidate[1][1], '{:.5f}'.format(candidate[0][0])) for candidate in cr[:1000]]))
    result_file.flush()


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

Tabulate


In [89]:
cutoffs = [1, 3, 20, 100, 1000]
print '{:30} {}'.format('Method', '  '.join(['{:4d}'.format(cutoff) for cutoff in cutoffs]))
for method, result_file in [('Levenshtein', 'levenshtein_generation_result.txt'), 
                            ('Levenshtein rank', 'levenshtein_generation_result_using_rank.txt'), 
                            ('Hacrf no transition', 'generation_result.txt'),
                            ('Hacrf no transition (pre-rank)', 'generation_result_with_pre_rank_no_transition.txt'),
                            ('Hacrf transition (pre-rank)', 'generation_result_with_pre_rank.txt')]:
    results = [line.split() for line in open(result_file, 'r').readlines()[:588]]
    results = [(line[1], [candidate[0] for candidate in eval(''.join(line[2:]))]) for line in results]
    result_indices = [line[1].index(line[0]) if line[0] in line[1] else 1001 for line in results]
    print '{:30} {}'.format(method, '  '.join(['{:.2f}'.format(sum([1.0 if res < cutoff else 0.0 for res in result_indices]) / len(result_indices))
                   for cutoff in cutoffs]))


Method                            1     3    20   100  1000
Levenshtein                    0.38  0.54  0.71  0.78  0.90
Levenshtein rank               0.47  0.61  0.74  0.82  0.91
Hacrf no transition            0.51  0.67  0.82  0.93  0.98
Hacrf no transition (pre-rank) 0.54  0.67  0.82  0.88  0.91
Hacrf transition (pre-rank)    0.49  0.65  0.84  0.90  0.91